<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="120px">
      <el-form-item label="触达标题" prop="cdName">
        <el-input
          v-model="queryParams.cdName"
          placeholder="请输入触达标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="触达逻辑类型" prop="cdType">
        <el-select v-model="queryParams.cdType" placeholder="请选择触达逻辑类型" clearable size="small">
          <el-option label="客户状态" value="STATUS" />
          <el-option label="日期" value="DAYS" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" border stripe :data="cdinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="唯一ID" align="center" prop="id" /> -->
      <el-table-column label="判断类型" align="center" prop="cdType" :formatter="cdTypeFormat"/>
      <el-table-column label="标题" align="center" prop="cdName" />
      <el-table-column label="客户装修进度" align="center" prop="cdCrmZxjd" :formatter="cdCrmZxjdFormat"/>
      <el-table-column label="品牌" align="center" prop="cdPpName" />
      <el-table-column label="添加时间" align="center" prop="createTime"/>
      <el-table-column label="添加人" align="center" prop="createBy"/>
      <el-table-column label="是否打开" align="center" prop="isActive" :formatter="isActiveFormat"/>
      <!-- <el-table-column label="触达客户状态" align="center" prop="cdCrmState" /> -->
      <!-- <el-table-column label="触达客户加好友天数" align="center" prop="cdAddDays" /> -->
      <!-- <el-table-column label="触达内容" align="center" prop="cdContent" /> -->
      <!-- <el-table-column label="触达小程序标题" align="center" prop="cdMiniTitle" /> -->
      <!-- <el-table-column label="触达小程序APPID" align="center" prop="cdMiniAppid" /> -->
      <!-- <el-table-column label="触达小程序URL" align="center" prop="cdMiniUrl" /> -->
      <!-- <el-table-column label="触达图片信息" align="center" prop="cdImgUrls" /> -->
      <el-table-column label="触发结果" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleSetActive(scope.row)"
          >{{ scope.row.isActive=='1'?'关闭':'打开' }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户触达配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form :model="recordsParams" ref="queryFormRecrods" :inline="true" label-width="120px">
        <el-form-item label="状态" prop="cdreYl4">
          <el-select v-model="recordsParams.cdreYl4" placeholder="请选择" clearable size="small">
            <el-option label="未发送" value="0" />
            <el-option label="已发送" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="消息日期" prop="createDate">
          <el-date-picker
            v-model="recordsParams.createDate"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryRecords">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-table border stripe :data="cdRecordsList">
        <el-table-column label="消息日期" align="center" prop="createTime"/>
        <el-table-column label="导购" align="center" prop="cdreYl2" />
        <el-table-column label="状态" align="center" prop="cdreYl4" :formatter="cdreYl4Format"/>
        <el-table-column label="发送日期" align="center" prop="updateTime" />
      </el-table>
      <pagination
        v-show="recordsTotal>0"
        :total="recordsTotal"
        :page.sync="recordsParams.pageNum"
        :limit.sync="recordsParams.pageSize"
        @pagination="getListRecords"
      />
    </el-dialog>
  </div>
</template>

<script>
import { setToken, getToken, setLesseeId } from '@/utils/auth'
import { listCdinfo, getCdinfo, delCdinfo, addCdinfo, updateCdinfo, exportCdinfo } from "@/api/khcd/cdinfo";
import { listCdrecords, getCdrecords, delCdrecords, addCdrecords, updateCdrecords, exportCdrecords } from "@/api/khcd/cdrecords";
import requestCrm from '@/utils/requestCrm'
export default {
  name: "Cdinfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      zxStatusOptions:[],
      // 客户触达配置表格数据
      cdinfoList: [],
      cdRecordsList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      recordsParams:{
        cdId:undefined,
        pageNum: 1,
        pageSize: 10,
        cdreYl4: undefined,
        createDate:undefined
      },
      recordsTotal:0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cdName: undefined,
        cdType: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      isActiveOptions: [],
    };
  },
  created() {
    let token = this.$route.query.token
    if(token){
    setToken(token)
    this.$store.commit('SET_TOKEN', token)
    setLesseeId('13')
        this.$store.commit('SET_LESSEEID', '13')
    }
    this.getDicts("is_active").then(response => {
      this.isActiveOptions = response.data;
    });
    this.getLookUpInfo('LABEL_INSTALL_PLAN', (res)=>{
          this.zxStatusOptions = res
      })
    this.getList();
  },
  methods: {
    // 是否有效字典翻译
    isActiveFormat(row, column) {
      return this.selectDictLabel(this.isActiveOptions, row.isActive);
    },
    getLookUpInfo(lookupcode, call){
      requestCrm({
          url:'WxService?method=LookupInfoByLookupCode',
          method: 'post',
          data: {
              lookupcode
          }
      }).then(res=>{
          if(call){
              call(res.data)
          }
      })
    },
    cdTypeFormat(row, column) {
      return row.cdType == "STATUS" ? "客户状态" : "日期";
    },
    cdCrmZxjdFormat(row, column) {
      return this.selectDictLabel(this.zxStatusOptions, row.cdCrmZxjd);
    },
    cdreYl4Format(row, column) {
      return row.cdreYl4 == "2" ? "已发送" : "未发送";
    },
    /** 查询客户触达配置列表 */
    getList() {
      this.loading = true;
      listCdinfo(this.queryParams).then(response => {
        this.cdinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    getListRecords(){
      console.log(this.recordsParams)
      const params = {
        pageNum: this.recordsParams.pageNum,
        pageSize: this.recordsParams.pageSize,
        cdId: this.recordsParams.cdId,
        cdreYl4: this.recordsParams.cdreYl4,
        // createDate: this.recordsParams.createDate,
        beginTime: (this.recordsParams.createDate&&this.recordsParams.createDate.length)?this.recordsParams.createDate[0]:'',
        endTime: (this.recordsParams.createDate&&this.recordsParams.createDate.length>1)?this.recordsParams.createDate[1]:'',
      }
      listCdrecords(params).then(response => {
        this.cdRecordsList = response.rows;
        this.recordsTotal = response.total;
        this.open = true;
      });
    },
    /** 搜索按钮操作 */
    handleQueryRecords() {
      this.recordsParams.pageNum = 1;
      this.getListRecords()
    },
    handleView(row) {
      this.title = "查看客户触达记录";
      this.recordsParams.cdId = row.id 
      this.getListRecords()
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加客户触达配置";
      this.$router.push('/cdinfoEdit') 
      
      //location.href = location.origin+'/#/cdinfoEdit'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      // const id = row.id || this.ids
      // getCdinfo(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改客户触达配置";
      // });
       this.$router.push('/cdinfoEdit?id=' + this.ids)
      // location.href = location.origin+'/#/cdinfoEdit?id=' + this.ids
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除客户触达配置编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCdinfo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有客户触达配置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCdinfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    handleSetActive(row) {
      this.$confirm('是否确认设置数据状态?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(()=> {
          return updateCdinfo({id:row.id, isActive:row.isActive=='1'?'0':'1'}).then(()=>{
            this.getList();
            this.msgSuccess("删除成功");
          });
        }).catch(()=> {});
    },
  }
};
</script>